﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelSoft.HotelManagement_DTO;
using System.Collections;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace HotelSoft.HotelManagement_DAO
{
    class LoaiphongDAO : AbstractDAO
    {
        public static DataTable SelectAll()
        {
            string sql = "SELECT * FROM LOAIPHONG";
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        public override bool Them(object AbstractDTO)
        {
            try
            {
                LoaiPhongDTO dto = (LoaiPhongDTO)AbstractDTO;
                string insertCommand = "INSERT INTO LOAIPHONG VALUES('" +
                   dto.TENLP + "', " +
                   dto.GIA + ", N'" +
                   dto.TRANGBI + "')";
                Config.AccessHelper.ExecuteQuery(insertCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }


        public override bool CapNhatThongTin(object AbstractDTO)
        {
            try
            {
                LoaiPhongDTO dto = (LoaiPhongDTO)AbstractDTO;
                string updateCommand = "UPDATE LOAIPHONG SET Gia = " + dto.GIA +
                     ", Trangbi =N'" + dto.TRANGBI +
                     "' WHERE TenLP = '" + dto.TENLP + "'";
                Config.AccessHelper.ExecuteQuery(updateCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public override void Xoa(object AbstractDTO)
        {
                LoaiPhongDTO dto = (LoaiPhongDTO)AbstractDTO;
                string deleteCommand = "DELETE FROM LOAIPHONG WHERE TenLP = '" + dto.TENLP +"'";
                Config.AccessHelper.ExecuteNonQuery(deleteCommand);
        }
        internal void Xoa(ArrayList dsPhong)
        {
            foreach (LoaiPhongDTO phong in dsPhong)
            {
                Xoa(phong);
            }
        }


        public bool SoSanhChuoi(String a, String b)
        {
            String a1 = a.ToLower();
            String b1 = b.ToLower();
            a1 = a1.Trim();
            b1 = b1.Trim();
            if (a1.Contains(b1))
                return true;
            return false;
        }
        public ArrayList TraCuuLoaiPhong(ArrayList dsLoaiphong, LoaiPhongDTO dtoPhongCanTraCuu)
        {
            ArrayList dsKetQuaTraCuu = new ArrayList();
            for (int i = 0; i < dsLoaiphong.Count; i++)
            {
                if (SoSanhTungPhong(((LoaiPhongDTO)dsLoaiphong[i]), dtoPhongCanTraCuu) == true)
                    dsKetQuaTraCuu.Add(((LoaiPhongDTO)dsLoaiphong[i]));
            }
            return dsKetQuaTraCuu;
        }
        public bool SoSanhTungPhong(LoaiPhongDTO dtoPhong1, LoaiPhongDTO dtoPhong2)
        {
            bool flag = false;
            if (dtoPhong2.TENLP != "")
                if (SoSanhChuoi(dtoPhong1.TENLP.ToString(), dtoPhong2.TENLP.ToString()) == true)
                    flag = true;
                else
                {
                    return false;
                }
           
            return flag;
        }


        public static DataTable SelectAll_Dieukien(object AbstractDTO)
        {
            LoaiPhongDTO dto = (LoaiPhongDTO)AbstractDTO;
            string sql = "SELECT * FROM LOAIPHONG Where TenLP='" + dto.TENLP + "'";
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        public ArrayList LayDanhSachLoaiPhong()
        {
            ArrayList arrDanhSach = new ArrayList();
            string searchCommand = "Select*from LOAIPHONG";
            DataTable ds = Config.AccessHelper.ExecuteQuery(searchCommand);
            LoaiPhongDTO phong;
            int i = 0;
            int iSoDong = ds.Rows.Count;
            int iSoCot = ds.Columns.Count;
            while (i < iSoDong)
            {
                phong = new LoaiPhongDTO();
                phong.TENLP = ds.Rows[i]["TenLP"].ToString();
                phong.GIA = float.Parse(ds.Rows[i]["Gia"].ToString());
                phong.TRANGBI = ds.Rows[i]["Trangbi"].ToString();
                
                arrDanhSach.Add(phong);
                i++;
            }
            return arrDanhSach;
        }

        public static List<LoaiPhongDTO> LayDSLoaiPhong()
        {
            List<LoaiPhongDTO> arrDanhSach = new List<LoaiPhongDTO>();
            string searchCommand = "Select*from LOAIPHONG";
            DataTable ds = Config.AccessHelper.ExecuteQuery(searchCommand);
            LoaiPhongDTO phong;
            int i = 0;
            int iSoDong = ds.Rows.Count;
            int iSoCot = ds.Columns.Count;
            while (i < iSoDong)
            {
                phong = new LoaiPhongDTO();
                phong.TENLP = ds.Rows[i]["TenLP"].ToString();
                phong.GIA = float.Parse(ds.Rows[i]["Gia"].ToString());
                phong.TRANGBI = ds.Rows[i]["Trangbi"].ToString();

                arrDanhSach.Add(phong);
                i++;
            }
            return arrDanhSach;
        }

        public override void TraCuu(object AbstractDTO)
        {
            throw new NotImplementedException();
        }

        public bool KTMA(object AbstractDTO)
        {
            LoaiPhongDTO dto = (LoaiPhongDTO)AbstractDTO;
            string strSQL;
            strSQL = "Select Count(*) From LOAIPHONG where TenLP='" + dto.TENLP + "'";
            return Config.AccessHelper.ExecuteScalar(strSQL);
        }
    }
}
